﻿using Microsoft.AspNetCore.Mvc;

namespace LeetCode.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SumController : ControllerBase
    {
        [HttpPost]
        public int[] TwoSum(int[] nums, int target)
        {
            List<int> result = new(2);
            List<int> listNums = nums.ToList();
            listNums.Sort();
            int l = 0;
            int r = nums.Length - 1;
            while (l < r)
            {
                int sum = listNums[l] + listNums[r];
                if (sum < target)
                {
                    l++;
                }
                else if (sum > target)
                {
                    r--;
                }
                if (sum == target)
                {
                    for (int i = 0; i < nums.Length; i++)
                    {
                        if (nums[i] == listNums[l])
                        {
                            result.Add(i);
                        }
                        else if (nums[i] == listNums[r])
                        {
                            result.Add(i);
                        }
                    }
                    break;
                }

            }
            return result.ToArray();

        }
    }
}
